//
//  BaseCollectionView.m
//  JKWDTeacher
//
//  Created by 学大教育 on 2022/1/18.
//

#import "BaseCollectionView.h"

@implementation BaseCollectionView
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
    }
    return self;
}
- (void)setSourceData:(NSArray *)sourceData
{
    _sourceData = sourceData;
    [self reloadData];
}
#pragma mark - UICollectionViewDataSource -
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _isMultiSection ? self.sourceData.count : 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger count = 0;
    if (_isMultiSection) {
        BaseSectionModel *sectionModel =  self.sourceData[section];
        count = sectionModel.cells.count;
    }else{
        count = self.sourceData.count;
    }
    return count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CellModel *model = nil;
    if (_isMultiSection) {
        BaseSectionModel *sectionModel =  self.sourceData[indexPath.section];
        model = sectionModel.cells[indexPath.item];
    }else{
        model = self.sourceData[indexPath.item];
    }
    BaseCollectionViewCell *cell = [BaseCollectionViewCell cellForModel:model collectionView:collectionView forIndexPath:indexPath];
    cell.indexPath = indexPath;
    cell.model = model;
    cell.delegate = self;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}
#pragma mark - UICollectionViewDelegate -
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(collectionView:didselectIndexPath:)]) {
        [_cellDelegate collectionView:collectionView didselectIndexPath:indexPath];
    }
}

- (void)actionIndexPath:(NSIndexPath *)indexPath infoData:(nullable id)data
{
    if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(collectionView:didActionIndexPath:infoData:)]) {
        [self.cellDelegate collectionView:self didActionIndexPath:indexPath infoData:data];
    }
}
#pragma mark -  UICollectionViewDelegateFlowLayout -
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CellModel *model = nil;
    if (_isMultiSection) {
        BaseSectionModel *sectionModel =  self.sourceData[indexPath.section];
        model = sectionModel.cells[indexPath.item];
    }else{
        model = self.sourceData[indexPath.item];
    }
    return model.itemSize;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    BaseSectionModel *sectionModel = _isMultiSection ? self.sourceData[section] : nil;
    return sectionModel.inset;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    BaseSectionModel *sectionModel = _isMultiSection ? self.sourceData[section] : nil;
    return sectionModel.minimumLineSpacing;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    BaseSectionModel *sectionModel = _isMultiSection ? self.sourceData[section] : nil;
    return sectionModel.minimumInteritemSpacing;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    BaseSectionModel *sectionModel = _isMultiSection ? self.sourceData[section] : nil;
    return sectionModel.sectionHeaderSize;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    BaseSectionModel *sectionModel = _isMultiSection ? self.sourceData[section] : nil;
    return sectionModel.sectionFooterSize;
}

@end
